const ls = require('local-storage')
const lsKey = 'zcj_user_info'
const expireMin = 60
const expireMs = 1000 * 60 * expireMin

const currentTimestamp = function () {
  return (new Date()).getTime()
}

module.exports = {
  get () {
    let userInfo = ls.get(lsKey)
    if (!userInfo) {
      return {}
    }

    if (userInfo && currentTimestamp() > userInfo.expire) {
      ls.remove(lsKey)
      return {}
    }

    return userInfo
  },
  set (userInfo) {
    userInfo.expire = currentTimestamp() + expireMs
    return ls.set(lsKey, userInfo)
  }
}
